using System.IO;
using Beaker.Services.FileSystem;
using Newtonsoft.Json;

namespace Beaker.OpenCube.Metadata
{
	public static class Metadata
	{
		/// <summary>
		/// Reads the json metadata from the given file
		/// </summary>
		/// <typeparam name="TMetadata">The type of the metadata in the file.</typeparam>
		/// <param name="file">The file to read the metadata from.</param>
		/// <returns></returns>
		public static TMetadata Read<TMetadata>( IFile file )
			where TMetadata : class
		{
			if( file == null )
				return null;

			using( TextReader reader = file.OpenText() )
			{
				var serializer = new JsonSerializer();
				return serializer.Deserialize<TMetadata>( new JsonTextReader( reader ) );
			}
		}
	}
}